package com.fenghuajueli.module_home.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fenghuajueli.module_home.bean.ToolsBean;
import com.fenghuajueli.module_home.databinding.ItemToolsPageBinding;

import java.util.List;

public class ToolsPageAdapter extends RecyclerView.Adapter<ToolsPageAdapter.MyViewHolder> {

    List<ToolsBean> toolsData;

    public ToolsPageAdapter(List<ToolsBean> toolsData) {
        this.toolsData = toolsData;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemToolsPageBinding binding = ItemToolsPageBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.binding.ivRes.setImageResource(toolsData.get(position).res);
        holder.binding.tvName.setText(toolsData.get(position).toolsName);
        holder.binding.ivRes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return toolsData.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final ItemToolsPageBinding binding;

        public MyViewHolder(@NonNull ItemToolsPageBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
